package com.fzj.example.collection;

import java.util.*;

/**
 * ClassName: MyList
 * date: 2019/10/14 14:21
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class MyList {
    public static void main(String[] args) {
        List<User> demoUserList=new ArrayList<>();
        demoUserList.add(new User("fzj",29));
        List<User> usersM=new ArrayList<>(demoUserList);
        System.out.println("集合比较："+demoUserList.equals(usersM));
        List<User> usersL=new ArrayList<>();
        for(int i=0;i<9999;i++){
            demoUserList.add(new User("cq",23));
        }
        System.out.println(demoUserList.size());
        System.out.println(demoUserList.isEmpty());
        //第一种方式
        long startTime=System.nanoTime();
        for(int i=0;i<demoUserList.size();i++){
            demoUserList.get(i);
        }
        long endTime=System.nanoTime();
        System.out.println("普通for模式："+ (endTime - startTime)/1000);
        startTime=System.nanoTime();
        for (User user: demoUserList ) {

        }
        endTime=System.nanoTime();
        System.out.println("新型forEach模式："+ (endTime - startTime)/1000);
        //第二种方式
        //Itr模式
        startTime=System.nanoTime();
        Iterator iterator=demoUserList.iterator();
        while (iterator.hasNext()){
            iterator.next();
        }
        endTime=System.nanoTime();
        System.out.println("Itr模式："+ (endTime - startTime)/1000);
        //listIterator模式
        startTime=System.nanoTime();
        ListIterator iterator2=demoUserList.listIterator();
        while (iterator2.hasNext()){
            iterator2.next();
        }
        endTime=System.nanoTime();
        System.out.println("listIterator模式："+ (endTime - startTime)/1000);

        /******************linedList模式***************************/
        System.out.println("linedList模式");

        List<Integer> linkedList = new LinkedList<>();
        for(int i = 0 ; i < 10000 ; i++){
            linkedList.add(i);
        }
        startTime = System.currentTimeMillis();
        for(int i = 0 ; i < 10000 ; i++){
            linkedList.get(i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("使用普通for模式："+ (endTime - startTime));

        startTime = System.currentTimeMillis();
        Iterator<Integer> it = linkedList.listIterator();
        while(it.hasNext()){
            it.next();
        }
        endTime = System.currentTimeMillis();
        System.out.println("listIterator模式："+ (endTime - startTime));
    }
}

class User{
    private String username;
    private int age;

    User(){}
    User(String username,int age){
        this.username=username;
        this.age=age;
    }
}
